package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class VideoController {

    @Autowired
    VideoManager videoManager;

    //推荐视频列表
    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        //1.获取分页参数和登陆者id
        Long userId = UserHolder.get().getId();
        //2.调用manager完成分页查询,接受返回值PageBeanVo
        PageBeanVo pageBeanVo = videoManager.findRecommendVideoByPage(userId,pageNum,pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    //保存视频
    @PostMapping("/smallVideos")
    public ResponseEntity save(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        //1.获取两个文件对象和登陆者id
        Long userId = UserHolder.get().getId();
        //2.调用manager完成保存操作
        videoManager.save(userId,videoThumbnail,videoFile);

        return ResponseEntity.ok(null);
    }

    //关注用户
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public ResponseEntity focusUser(@PathVariable Long focusUserId){
        return videoManager.focusUser(focusUserId);
    }

    //取关
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public ResponseEntity unFocusUser(@PathVariable Long focusUserId){
        return videoManager.unFocusUser(focusUserId);
    }
}
